﻿using System.Text;
using Microsoft.Practices.ServiceLocation;
using Pap.Core.Logging;
using Pap.Core.Sync;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Administration.Views
{
    using System;

    using Coolite.Ext.Web;

    public partial class SynchronizationUI : System.Web.UI.Page, IPermittedPage
    {
        private static SyncAgent syncAgent;
        private StringBuilder strbld;

        #region Properties

        public string PermissionName
        {
            get { return "Synch"; }
        }

        #endregion Properties

        #region Events

        protected void btnSync_Clicked(object sender, AjaxEventArgs e)
        {
            btnSync.Disabled = true;
            try
            {
                if (syncAgent == null)
                {
                    syncAgent = new SyncAgent();
                    strbld = new StringBuilder();
                    syncAgent.SyncProgress += syncAgent_SyncProgress;
                    syncAgent.TableSync += syncAgent_TableSync;
                    syncAgent.Syncronize();
                    this.uxSyncLogPanel.UpdateBody(strbld.ToString());
                }
               else
                    throw new InvalidOperationException("Sync is in use");
            }
            catch(Exception ex)
            {
                strbld.Append("Error Accured " + ex.ToString());
                strbld.Append("<br />");
                ServiceLocator.Current.GetInstance<ILog>().RaiseError(ex);
                this.uxSyncLogPanel.UpdateBody(strbld.ToString());
            }
            finally
            {
                syncAgent = null;
            }
            btnSync.Disabled = false;
        }

        protected void syncAgent_TableSync(object sender, SyncTableProgressEventArgs e)
        {
            strbld.AppendFormat("Table {0}, Server changes {1} - Local changes {2} {3}", e.TableName,
                                e.LocalChangesCount, e.ServerChangesCount, "<br />");
            
        }

        protected void syncAgent_SyncProgress(object sender, SyncAgentProgressEventArgs e)
        {
            switch (e.Stage)
            {
                case SyncStage.BeginSession:
                    strbld.Append("<b>Syncronization Started</b> <br />");
                    strbld.AppendFormat("Syncronizting ({0}) table(s) <br />", e.TotalNumberOfTables);
                    break;
                case SyncStage.EndSession:
                    strbld.Append("<b>Syncronization Ended</b> <br />");
                    break;
                case SyncStage.ApplyTableChanges:
                    strbld.Append(e.Percentage + "% Completed <br />");
                    break;
                default:
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // check if this is a main committee
        }

        protected void Task_UpdateProgress(object sender,AjaxEventArgs e)
        {
            
        }

        #endregion Events
    }
}